<?php

/*
 * BaseFile class allow action on file
 */
class BaseFile
{
	protected $uri;
	protected $name;
	
	// Make a new instance of the file
	public function __construct($uri, $name=null)
	{
		$this->uri = $uri;
		if(is_null($name))
		{
			$this->name = $uri; //basename($this->uri);
		}
		else
		{
			$this->name = $name;
		}
	}
	
	// Get the file URI
	public function getURI()
	{
		return $this->uri;
	}
	
	// Get the file name
	public function getName()
	{
		return $this->name;
	}
	
	// Set the file name
	public function setName($n)
	{
		$this->name = $n;
	}
	
	// Return true if the file is local (basic http eregi test)
	//TODO improvement
	public function isLocal()
	{
		return (!ereg('^http://', $this->uri));
	}
	
	/**
	 * Test si le fichier exists (voir file_exists).
	 * Marche aussi avec les URLs
	 *
	 * @param string $fichier url ou chemin du fichier a tester
	 * @param &bool $isFichier true si c'est un fichier false si c'est une url
	 * @return bool $e
	 */
	public function exists()
	{

		if(@fclose(@fopen($this->uri, 'r')))
		{
			return true;
		}

		return false;
	}
	
	/**
	 * Retourne l'extention du fichier
	 *
	 * @param string $nomFichier nom du fichier
	 * @return string $ext extention trouvee (vide si pas d'extention)
	 */
	public function getExtension()
	{
		if(!eregi('\.', $this->name))
		{
			return '';
		}
		
		return strtolower(substr(strrchr(basename($this->name), "."), 1));
	}
	
	// Return the mime type of the file
	public function getMimeType()
	{
		if(!function_exists('mime_content_type'))
		{
			function mime_content_type($file)
			{
				$extention = strtolower(substr(strrchr(basename($file), "."), 1));

				$mime_type_ary = array(
					'css'  => 'text/css',
					'html' => 'text/html',
					'htm'  => 'text/html',
					'js'   => 'text/javascript',
					'txt'  => 'text/plain',
					'rtx'  => 'text/richtext',
					'tsv'  => 'text/tab-separated-value',
					'xml'  => 'text/xml',
					'xls'  => 'text/xml',

					'eml'  => 'message/rfc822',
					'nws'  => 'message/rfc822',

					'bmp'  => 'image/bmp',
					'pcx'  => 'image/bmp',
					'gif'  => 'image/gif',
					'ief'  => 'image/ief',
					'jpeg' => 'image/jpeg',
					'jpg'  => 'image/jpeg',
					'jpe'  => 'image/jpeg',
					'png'  => 'image/png',
					'tiff' => 'image/tiff',
					'tif'  => 'image/tiff',
					'cmu'  => 'image/x-cmu-raster',
					'pnm'  => 'image/x-portable-anymap',
					'pbm'  => 'image/x-portable-bitmap',
					'pgm'  => 'image/x-portable-graymap',
					'ppm'  => 'image/x-portable-pixmap',
					'rgb'  => 'image/x-rgb',
					'xbm'  => 'image/x-xbitmap',
					'xpm'  => 'image/x-xpixmap',
					'xwd'  => 'image/x-xwindowdump',

					'dwg'  => 'application/acad',
					'ccad' => 'application/clariscad',
					'drw'  => 'application/drafting',
					'dxf'  => 'application/dxf',
					'xls'  => 'application/excel',
					'hdf'  => 'application/hdf',
					'unv'  => 'application/i-deas',
					'igs'  => 'application/iges',
					'iges' => 'application/iges',
					'doc'  => 'application/msword',
					'dot'  => 'application/msword',
					'wrd'  => 'application/msword',
					'oda'  => 'application/oda',
					'pdf'  => 'application/pdf',
					'ppt'  => 'application/powerpoint',
					'ai'   => 'application/postscript',
					'eps'  => 'application/postscript',
					'ps'   => 'application/postscript',
					'rtf'  => 'application/rtf',
					'rm'   => 'application/vnd.rn-realmedia',
					'dvi'  => 'application/x-dvi',
					'gtar' => 'application/x-gtar',
					'tgz'  => 'application/x-gtar',
					'swf'  => 'application/x-shockwave-flash',
					'tar'  => 'application/x-tar',
					'gz'   => 'application/x-gzip-compressed',
					'zip'  => 'application/zip',
					'xhtml'=> 'application/xhtml+xml',
					'xht'  => 'application/xhtml+xml',

					'au'   => 'audio/basic',
					'snd'  => 'audio/basic',
					'aif'  => 'audio/x-aiff',
					'aiff' => 'audio/x-aiff',
					'aifc' => 'audio/x-aiff',
					'wma'  => 'audio/x-ms-wma',

					'mpeg' => 'video/mpeg',
					'mpg'  => 'video/mpeg',
					'mpe'  => 'video/mpeg',
					'mov'  => 'video/quicktime',
					'avi'  => 'video/msvideo',
					'movie'=> 'video/x-sgi-movie',

					'unknow' => 'application/octet-stream'
				);

				return (!empty($mime_type_ary[$extention])) ? $mime_type_ary[$extention] : $mime_type_ary['unknow'];
			}
		}
		
		return mime_content_type($this->uri);
	}
	
	/**
	 * Fonction de calcul md5 de fichier très très très rapide
	 * mais elle n'est pas aussi fiable que le md5_file
	 *
	 * @param $fichier nom de fichier
	 * @return md5
	 */
	public function fastHash($algo='md5')
	{
		if($this->isLocal() && filesize($this->uri) < 120*5000 )
		{
			return hash_file($algo, $this->uri);
		}
		
		$f = fopen($this->uri, 'r');
		$c = 0;
		$b = '';
		
		while(!feof($f) && $c++ < 100)
		{
			$b .= fgets($f, 4096);
		}
		fseek($f, filesize($this->uri) - 5000);
		$b .= fgets($f, 4096);
		fclose($f);
		
		return hash($algo, $b);
	}
	
	/**
	 * Transforme un nom de fichier en nom de fichier spécial
	 *
	 * @return string $n nom de fichier
	 */
	public function getSafeFileName()
	{
		return strtolower(time().'-'.ereg_replace('[^a-zA-Z0-9\.]', '_', debug(basename($this->name), 'Name used for the safe name', JGLIB_DEBUG_JLIB)));
	}

	/**
	 * Récupère le nom de fichier en enlevant l'extention et le chemin d'accès.
	 * effectue d'autres petites transformations pour rendre le nom plus lisible
	 *
	 * @return string $n nom
	 */
	public function getReadableFileName()
	{
		$tmp1 = trim(basename(str_replace('_', ' ', $this->name)));
		$tmp2 = explode('.', $tmp1);
		if(count($tmp2) > 1) array_pop($tmp2);
		return implode('.', $tmp2);
	}
	
	// Return the string view of the file object
	public function __toString()
	{
		return $this->name.' ('.$this->uri.')';
	}
}
